{% extends "base.html" %}
{% block title %}Vnos končne ocene{% endblock %}

{% block js %}
<script type="text/javascript">


function is_int(value){
  if((parseFloat(value) == parseInt(value)) && !isNaN(value)){
      return true;
  } else {
      return false;
  }
}

function checkSubmit(){
  s = $("input.error").size();
  if (s > 0){
    alert("Odpravi napake pred shranjevanjem.");
    return false;
  }else {
    return true;
  }
}


$(document).ready(function(){
  $(".input_vp").change(function(){
    var checked = $(this).is(':checked');
    $(".input_grade",$(this).parent().parent()).each(function(index,el){
      if (checked) {
        $(this).attr('disabled', 'disabled');
        $(this).val("");
        $(this).parent().parent().removeClass("error");
      }else {
        $(this).removeAttr('disabled');
      }
    });
    
  });

  $("#id_exam").change(function(){
    selected_value = $('#id_exam option:selected').val();
    if (selected_value != undefined && selected_value != "") {
      document.location="/exams/grade/{{ professor.pk }}/"+selected_value+"/";
    }
  });
  
  
  $("#form_grade").submit(function(){
    $(".info_msg").hide();
    grade_type = "{{exam.rating_type}}";
    must_exam_grade = false;
    must_practice_grade = false;
    error = false;
    if (grade_type == 'OC1' || grade_type == 'OC2'){
      must_practice_grade = true;
    }
    
    if (grade_type == 'OC1' || grade_type == 'OC3'){
      must_exam_grade = true;
    }
    
    $(".list tr").each(function(index, el){
      if (index == 0){
        return ;
      }
      practice_grade_set = false;
      exam_grade_set = false;
      practice_grade_value = 0;
      exam_grade_value = 0;
      zeroSet1 = false;
      zeroSet2 = false;
      if ($("td input",el).size() > 0) {
        $("td input",el).each(function(index, el1){
          value = $(el1).val();
          if (must_practice_grade && $(el1).hasClass("practice_grade")){
            if (value.length > 0){
              practice_grade_set = true;
              practice_grade_value = value;
            }else if(value.length == 0){
              zeroSet1 = true;
            }
          }
          
          if (must_exam_grade && $(el1).hasClass("exam_grade")){
            if (value.length > 0){
              exam_grade_set = true;
              exam_grade_value = value; 
            }else if(value.length == 0){
              zeroSet2 = true;
            }
          }
                 
        });
        
        if (must_exam_grade && must_practice_grade){
          if (zeroSet1 && zeroSet2) {
            
          }else{
            //morajo bit obe oceni vpisani
            if (!(practice_grade_set && exam_grade_set)){
              $(el).addClass("error");
              error = true;
            } else {
              //obe oceni morajo biti pozitivnia li obe negativni
              if (!((exam_grade_value > 5 && practice_grade_value > 5) || (exam_grade_value < 6  && practice_grade_value < 6))){
                $(el).addClass("error");
                error = true;          
              }else{
                $(el).removeClass("error");
              }
            }
          }
        } 
      }

        
    });
    
    if (error){
      alert("Trenutni vnos vsebuje napake!");
      return false;
    }

  
  });
  
  
  $(".input_grade").change(function(){
    
    value = $(this).val();
    if (value.length > 0){
      if (is_int(value)){
        value = parseInt(value);
        if (value <= 0 || value > 10) {
          alert("Ni veljavna ocena!");
          $(this).addClass("error");
        }else {
          $(this).removeClass("error");
        }
      }else {
        alert("Ni veljavna ocena!");
        $(this).addClass("error");
      }
    }else {
      $(this).removeClass("error");
    }  

  });
  
});



</script>
{% endblock %}

{% block content %}

{% if only_search %}

<script type="text/javascript" src="{{ MEDIA_URL }}js/search.js"></script>
<script type="text/javascript">

$(document).ready(function(){    
    search_setcb('professor',function(professor){
      url = '/exams/grade/'+professor.pk+'/';
      document.location = url;
    });
    
});
</script>
{% include "snippets/professor/search.htm" %}
{% else %}
<p>
  <b>Izbrani profesor: </b> {{ professor }}<br />
  <a href="/exams/grade/">Ponovna izbira profesorja</a>
</p><br />
<form method="post">
{% csrf_token %}
	{{ form.as_p }}
</form>
{% if data != None %}

<h2>Seznam študentov</h2>
<p><b>Seznam za izpit:</b> {{ exam }} <br />
   <b>Izvajalec:</b> {{ exam.show_professor }}
</p>
{% if saved %}
<div class="info_msg">Podatki so bili shranjeni!</div>
{% endif %}
<form id="form_grade" method="POST" onSubmit="javascript: return checkSubmit();">
{% csrf_token %}
<table class="list">
  <tr>
    <th class="num">#</th>
    <th>Vpisna številka</th>
    <th>Priimek ime</th>
    <th>Št. polaganj</th>
    <th>Študijsko leto</th>
    <th>Točke</th>
    <th>Točke ostalo</th>
    {% if exam.rating_type == "OC3" or exam.rating_type == "OC1" %}
    <th>Ocena izpita</th>
    {% endif %}
    {% if exam.rating_type == "OC2" or exam.rating_type == "OC1" %}
    <th>Ocena vaj</th>
    {% endif %}
    <th>VP</th>
  </tr>
  {% for a in data %}
  <tr>
    <td>{{ forloop.counter }}</td>
    <td>{{ a.student.enrollment_number }}</td>
    <td>{{ a.student.user.last_name }} {{ a.student.user.first_name }}</td>
    <td>{{ a.num_app }}</td>
    <td>{{ a.exam.subject.school_year_start }} / {{ a.exam.subject.school_year_end }}</td>
    <td>{% if a.points %}{{ a.points }}{% else %}/{% endif %}</td>
    <td>{% if a.points_rest %}{{ a.points_rest }}{% else %}/{% endif %}</td>
    {% if exam.rating_type == "OC3" or exam.rating_type == "OC1" %}
    <td><input {% if a.exam_grade == 0 or a.practice_grade == 0 %} disabled="disabled" {% endif %} class="input_grade exam_grade" type="text" value="{% if a.exam_grade %}{{ a.exam_grade }}{% endif %}" name="application.{{ a.id }}.exam_grade" /></td>
    {% endif %}
    {% if exam.rating_type == "OC2" or exam.rating_type == "OC1" %}
    <td><input {% if a.exam_grade == 0 or a.practice_grade == 0 %} disabled="disabled" {% endif %} class="input_grade practice_grade" type="text" value="{% if a.practice_grade %}{{ a.practice_grade }}{% endif %}" name="application.{{ a.id }}.practice_grade" /></td>
    {% endif %}
    <td><input class="input_vp" {% if a.exam_grade == 0 or a.practice_grade == 0 %}checked="checked"{% endif %} type="checkbox" name="application.{{ a.id }}.vp" value="1" /></td>
 </tr>
  {% endfor %}
</table>
{% if data.count > 0 %}
<input type="submit" value="Shrani" />
{% endif %}
</form>
{% endif %}
{% endif %}
{% endblock %}